package com.tudouji.project.harvest.controller;

import com.alibaba.fastjson.JSONObject;
import com.tudouji.common.utils.http.HttpBusinessUtils;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.config.DwlConfig;
import com.tudouji.framework.interceptor.annotation.RepeatSubmit;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.harvest.domain.Procurementcontract;
import com.tudouji.project.harvest.mapper.ProcurementcontractMapper;
import com.tudouji.project.harvest.service.IProcurementcontractService;
import com.tudouji.project.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合同管理Controller
 *
 * @author tudouji
 * @date 2021-04-07
 */
@RestController
@RequestMapping("/harvest/procurementcontract")
public class ProcurementcontractController extends BaseController
{
    @Autowired
    private IProcurementcontractService procurementcontractService;
    @Autowired
    private ProcurementcontractMapper procurementcontractMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询合同管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Procurementcontract procurementcontract, String status, String functionType)
    {
        List<Procurementcontract> list = procurementcontractService.selectProcurementcontractList(procurementcontract, status, functionType);
        return getDataTable(list);
    }

    /**
     * 查询合同管理列表
     */
    @GetMapping("/getAll")
    public AjaxResult getAll(Procurementcontract procurementcontract)
    {
        List<Procurementcontract> list = procurementcontractMapper.selectProcurementcontractList(procurementcontract);
        return AjaxResult.success(list);
    }

    /**
     * 导出合同管理列表
     */
    @PreAuthorize("@ss.hasPermi('harvest:procurementcontract:export')")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Procurementcontract procurementcontract, String status, String functionType)
    {
        List<Procurementcontract> list = procurementcontractService.selectProcurementcontractList(procurementcontract, status, functionType);
        ExcelUtil<Procurementcontract> util = new ExcelUtil<Procurementcontract>(Procurementcontract.class);
        return util.exportExcel(list, "procurementcontract");
    }

    /**
     * 获取合同管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(procurementcontractMapper.selectProcurementcontractByNewId(id));
    }

    /**
     * 新增合同管理
     */
    @PreAuthorize("@ss.hasPermi('harvest:procurementcontract:add')")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Procurementcontract procurementcontract)
    {
        return toAjax(procurementcontractService.insertProcurementcontract(procurementcontract));
    }

    /**
     * 修改合同管理
     */
    @PreAuthorize("@ss.hasPermi('harvest:procurementcontract:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Procurementcontract procurementcontract)
    {
        return toAjax(procurementcontractService.updateProcurementcontract(procurementcontract));
    }

    /**
     * 删除合同管理
     */
    @PreAuthorize("@ss.hasPermi('harvest:procurementcontract:remove')")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(procurementcontractService.deleteProcurementcontractByIds(guids));
    }


    /**
     * 发起
     **/
    @RepeatSubmit
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Procurementcontract entity, String fileIds, @RequestParam(required = false)Long taskid
            , @RequestParam(required = false)String opinion) {
        return procurementcontractService.save(entity, fileIds, taskid, opinion);
    }

    /**
     * 审批
     **/
    @RepeatSubmit
    @PostMapping("/approval")
    public AjaxResult  approval(Long id,Long taskid,String opinion,String fileIds){
        return procurementcontractService.approval(id,taskid,opinion,fileIds);
    }

    /**
     * 审批
     **/
    @RepeatSubmit
    @PostMapping("/finance")
    public AjaxResult  finance(Long id,Long taskid,String opinion,String fileIds, @RequestBody Procurementcontract entity){
        return procurementcontractService.finance(id,taskid,opinion,fileIds,entity);
    }

    /**
     * 采收完成，结束采收合同
     **/
    @RepeatSubmit
    @PostMapping("/finish")
    public AjaxResult  finish(Long id){
        return procurementcontractService.finish(id);
    }

    @RepeatSubmit
    @Log(title = "拒绝", businessType = BusinessType.OTHER)
    @PostMapping("/refuse")
    public AjaxResult refuse(Long taskid,Long id,String opinion,String fileIds,Long refuseNodeid,Long backUserid){
        return procurementcontractService.refuse(id, fileIds, opinion, taskid,refuseNodeid,backUserid);
    }

    /**
     * 微信--删除
     */
    @RepeatSubmit
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("del")
    public AjaxResult remove(Long id) {
        return procurementcontractService.del(id);
    }

    /**
     * 通过采收统一货源与合同编号获取拉货二维码
     */
    @GetMapping("/supplyScanCode")
    public AjaxResult supplyScanCode(Long contractid, @RequestParam(required = false) String supplyid)
    {
//        if (StringUtils.isEmpty(supplyid)){
            supplyid = configService.selectConfigByKey("supplyid");
//        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("supplyid", supplyid);
        map.put("contractcode", contractid+"");
        Map<String, String> resultmap = HttpBusinessUtils.harvestPost(map, DwlConfig.getWebPath() + "/weixin/Erweima!supplyScanCodeI.action");
        String result = resultmap.get("result");
        JSONObject resultObj = JSONObject.parseObject(result);
        String re = resultObj.getString("result");
        if (re.startsWith("error")) {
            return AjaxResult.error("物流平台生成二维码失败：" + re);
        } else if ("signerro".equals(re)) {
            return AjaxResult.error("物流平台生成二维码Token验证失败");
        } else if ("error".equals(re)) {
            return AjaxResult.error("物流平台生成二维码接口异常");
        } else {
            return AjaxResult.success(re);
        }
    }


}
